---
title: Rust to JS Type Conversion
description: Reference for how Anchor converts between Rust and TypeScript types
---

This reference shows to converts between Rust and TypeScript types.

## Primitive Types

### Boolean

| Rust   | TypeScript | Example |
| ------ | ---------- | ------- |
| `bool` | `boolean`  | `true`  |

### Numbers

| Rust                    | TypeScript  | Example             |
| ----------------------- | ----------- | ------------------- |
| `u8/u16/u32/i8/i16/i32` | `number`    | `99`                |
| `u64/u128/i64/i128`     | `anchor.BN` | `new anchor.BN(99)` |
| `f32/f64`               | `number`    | `1.0`               |

### Strings

| Rust     | TypeScript | Example   |
| -------- | ---------- | --------- |
| `String` | `string`   | `"hello"` |

## Collections

### Arrays and Vectors

| Rust                   | TypeScript | Example     |
| ---------------------- | ---------- | ----------- |
| `[T; N]` (fixed array) | `Array<T>` | `[1, 2, 3]` |
| `Vec<T>` (vector)      | `Array<T>` | `[1, 2, 3]` |

### Optional Values

| Rust        | TypeScript               | Example                       |
| ----------- | ------------------------ | ----------------------------- |
| `Option<T>` | `T \| null \| undefined` | `null` (None)<br/>`42` (Some) |

## Complex Types

### Structs

```rust title="Rust"
// Rust
struct MyStruct {
    val: u16,
}
```

```typescript title="TypeScript"
// TypeScript
type MyStruct = {
  val: number;
};

// Example
const instance = { val: 99 };
```

### Enums

```rust title="Rust"
// Rust
enum MyEnum {
    One,
    Two { val: u32 },
    Three(u8, i16),
}
```

```typescript title="TypeScript"
// TypeScript Representations
// Unit variant
const one = { one: {} };

// Named variant
const two = {
  two: { val: 99 },
};

// Tuple variant
const three = {
  three: [12, -34],
};
```

## Notes

- Rust integers (`u8` through `i32`) map to JavaScript `number`
- Larger integers (`u64` and above) use Anchor's `BN` type for precision
- Rust's `Option<T>` maps to TypeScript's union type with `null`/`undefined`
- Structs and enums become JavaScript objects
